package com.example.concurrency.scheduled;


import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ZhenWuWang
 */
//@Component
//@EnableAsync
public class ScheduledDemo
{
    private AtomicInteger num = new AtomicInteger(0);

    @Scheduled(cron = "${task.device_syn_time:0/5 * * * * ? }")
    // 如果再等待过程中 手动执行了方法 fixedRate等待时间会更新么？ 如果不会，怎么样更新？
    @Scheduled(fixedRate = 5000)
//    @Async
    public void doTask()
    {
        int n = num.incrementAndGet();
        System.out.println(Thread.currentThread().getName() + " --- num: " + n);
        try
        {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " === num: " + n);
    }

    @Scheduled(cron = "${task.device_syn_time:0/5 * * * * ? }")
    public void doTask2()
    {
        int n = num.incrementAndGet();
        System.out.println(Thread.currentThread().getName() + " +++ num: " + n);
        try
        {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " >>> num: " + n);
    }
}
